home *** CD-ROM | disk | FTP | other *** search
- #ifndef __STD_RW_LOCNUMRW__
- #define __STD_RW_LOCNUMRW__
- #pragma option push -b -a4 -Vx- -Ve- -w-inl -w-aus -w-sig
-
- /****************************************************************************
- *
- * rw/numbrw - Internal classes for numeric formatting and parsing.
- *
- * $Id: numbrw,v 1.19 1996/10/22 05:12:47 delaney Exp $
- *
- ***************************************************************************
- *
- * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
- * ALL RIGHTS RESERVED *
- * The software and information contained herein are proprietary to, and
- * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
- * intends to preserve as trade secrets such software and information.
- * This software is furnished pursuant to a written license agreement and
- * may be used, copied, transmitted, and stored only in accordance with
- * the terms of such license and with the inclusion of the above copyright
- * notice. This software and information or any other copies thereof may
- * not be provided or otherwise made available to any other person.
- *
- * Notwithstanding any other lease or license that may pertain to, or
- * accompany the delivery of, this computer software and information, the
- * rights of the Government regarding its use, reproduction and disclosure
- * are as set forth in Section 52.227-19 of the FARS Computer
- * Software-Restricted Rights clause.
- *
- * Use, duplication, or disclosure by the Government is subject to
- * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
- * Technical Data and Computer Software clause at DFARS 252.227-7013.
- * Contractor/Manufacturer is Rogue Wave Software, Inc.,
- * P.O. Box 2328, Corvallis, Oregon 97339.
- *
- * This computer software and information is distributed with "restricted
- * rights." Use, duplication or disclosure is subject to restrictions as
- * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
- * Computer Software-Restricted Rights (April 1985)." If the Clause at
- * 18-52.227-74 "Rights in Data General" is specified in the contract,
- * then the "Alternate III" clause applies.
- *
- ***************************************************************************/
-
-
- #ifndef _RWSTD_NO_NAMESPACE
- namespace __rwstd {
- #endif
-
-
- // -------------------------------------------------------
- // Implementation class template -- timepunct_init<charT>.
- // -------------------------------------------------------
-
- // Structure used to initialize the private data members of timepunct<charT>.
-
- template <class charT>
- class _RWSTDExportTemplate timepunct_init {
- public:
- const charT *dn_[7][2]; // Day names and abbreviations
- const charT *mn_[12][2]; // Month names and abbreviations
- const charT *ampm_[2]; // AM/PM strings
- const charT *bad_; // Bad value output signal
- const charT *ord_[100]; // Ordinal numbers
- const charT *pat_[6]; // Patterns for formats "xXcDrT" resp.
- };
-
- // -------------------------------------------------------
- // Implementation class template -- timepunct_data<charT>.
- // -------------------------------------------------------
-
- // timepunct<charT> derives from this (via rwstd::timepunct_impl) to get its
- // private data members.
-
- template <class charT>
- class _RWSTDExportTemplate timepunct_data {
- friend class timepunct<charT>;
- friend class keyword_cracker<charT>;
- public:
- typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
- private:
-
- string_type dn_[7][2];
- string_type mn_[12][2];
- string_type ampm_[2];
- string_type bad_;
- string_type ord_[100];
- string_type pat_[6];
-
- keyword_def<charT> dn_defs_[7][2];
- keyword_map<charT> dn_map_;
- keyword_def<charT> mn_defs_[12][2];
- keyword_map<charT> mn_map_;
-
- protected:
- timepunct_data (const timepunct_init<charT> &i);
- void rw_init (void);
- };
-
- // -------------------------------------------------------
- // Implementation class template -- timepunct_impl<charT>.
- // -------------------------------------------------------
-
- // timepunct<charT> derives from this to get the parts of its behavior that
- // must be specialized on the character type. Currently the only specialized
- // behavior is the initialization logic in the constructor.
-
- template <class charT>
- class _RWSTDExportTemplate timepunct_impl :
- public timepunct_data<charT>
- {
- protected:
- timepunct_impl(void);
- };
-
- _RWSTD_TEMPLATE
- class _RWSTDExport timepunct_impl<char>: // Specialization
- public timepunct_data<char>
- {
- static timepunct_init<char> ivals_; // Vendor-supplied
- protected:
- timepunct_impl (void):
- timepunct_data<char>(ivals_) { }
- };
-
- #ifndef _RWSTD_NO_WIDE_CHAR
- _RWSTD_TEMPLATE
- class _RWSTDExport timepunct_impl<wchar_t>: // Specialization
- public timepunct_data<wchar_t>
- {
- static timepunct_init<wchar_t> ivals_; // Vendor-supplied
- protected:
- timepunct_impl(void):
- timepunct_data<wchar_t>(ivals_) { }
- };
- #endif // _RWSTD_NO_WIDE_CHAR
-
-
- // ----------------------------------------------------
- // Time and date punctuation facet -- timepunct<charT>.
- // ----------------------------------------------------
-
- template <class charT>
- class timepunct : public locale::facet,
- public timepunct_impl<charT>
- {
- public:
- typedef charT char_type;
- typedef basic_string<charT,char_traits<charT>,allocator<charT> >
- string_type;
-
- _EXPLICIT timepunct (size_t refs=0):
- locale::facet(refs,locale::time) { }
-
- string_type dayname (int day, bool abbreviated) const
- { return do_dayname(day,abbreviated); }
- string_type monthname (int month, bool abbreviated) const
- { return do_monthname(month,abbreviated); }
- string_type ordinal (int number) const
- { return do_ordinal(number); }
-
- static locale::id id;
-
- // Implementation:
- enum { facet_cat_ = locale::time, ok_implicit_ = 1 };
-
- protected:
- virtual ~timepunct() { }
-
- virtual string_type do_dayname (int day, bool abbreviated) const;
- virtual string_type do_monthname (int month, bool abbreviated) const;
- virtual string_type do_ordinal (int number) const;
-
- private:
- void rw_init (void);
-
- #ifdef _RWSTD_NO_MEMBER_TEMPLATES
- locale::id &get_id (void) const { return id; }
- #endif
- };
-
-
- // --------------------------------------------------------
- // Implementation class template -- keyword_cracker<charT>.
- // --------------------------------------------------------
-
- // Gives derived classes a backdoor way to get the private keyword_map and
- // other members out of numpunct and timepunct facets.
-
- template <class charT>
- class _RWSTDExportTemplate keyword_cracker {
- public:
- static const keyword_map<charT>& get_tf_map
- (const numpunct<charT> &nump)
- { return nump.tf_map_; }
- static const keyword_map<charT>& get_day_map
- (const timepunct<charT>& timp)
- { return timp.dn_map_; }
- static const keyword_map<charT>& get_month_map
- (const timepunct<charT>& timp)
- { return timp.mn_map_; }
-
- static const _TYPENAME numpunct<charT>::string_type& get_tf_string
- (const numpunct<charT>& nump,bool v)
- { return v? nump.tn_ : nump.fn_; }
- static const _TYPENAME timepunct<charT>::string_type&
- get_day_string
- (const timepunct<charT>& timp,unsigned n,bool abbr)
- { return n<7? timp.dn_[n][abbr] : timp.bad_; }
- static const _TYPENAME timepunct<charT>::string_type&
- get_month_string
- (const timepunct<charT>& timp,unsigned n,bool abbr)
- { return n<12? timp.mn_[n][abbr] : timp.bad_; }
- static const _TYPENAME timepunct<charT>::string_type&
- get_ampm_string
- (const timepunct<charT>& timp,bool pm)
- { return timp.ampm_[pm]; }
- static const _TYPENAME timepunct<charT>::string_type&
- get_ord_string
- (const timepunct<charT>& timp,unsigned n)
- { return n<99? timp.ord_[n] : timp.bad_; }
- static const _TYPENAME timepunct<charT>::string_type&
- get_patt_string
- (const timepunct<charT>& timp,unsigned n)
- { return n<6? timp.pat_[n] : timp.bad_; }
- };
-
- // -------------------------------------------------------------
- // Implementation class template -- digit_handler_base_1<charT>.
- // -------------------------------------------------------------
-
- // Contains charT-dependent functionality that is common to the digit_reader
- // and digit_writer classes, particularly related to getting access to ctype
- // and punct facets they both use.
-
- template <class charT>
- class _RWSTDExportTemplate digit_handler_base_1:
- public keyword_cracker<charT>
- {
- public:
- const ctype<charT> &ctyp;
- const digit_map<charT>& dmap;
- const punct_data<charT>& punct;
-
- // Constructor for general numerics, gets punct from loc's numpunct facet.
- digit_handler_base_1 (const locale &loc);
-
- // Constructor for money, passed punct_data is in fact a moneypunct facet.
- digit_handler_base_1 (const locale &loc,const punct_data<charT>&);
- };
-
- // ------------------------------------------
- // Implementation class -- digit_reader_base.
- // ------------------------------------------
-
- // Contains digit_reader members that do not depend on the digit_reader
- // template parameters, particularly for converting strings of char digits
- // to long and floating-point binary values.
-
- class _RWSTDExport digit_reader_base : public digit_map_base
- {
- public:
- enum {
- good = 0x0000,
- no_digits = 0x0001,
- too_many_digits = 0x0002,
- too_many_groups = 0x0004,
- group_too_long = 0x0008,
- group_is_empty = 0x0010,
- bad_grouping = 0x0020,
- bad_exponent = 0x0040,
- overflow = 0x0080,
- bad_curr_symbol = 0x0100,
- bad_sign = 0x0200
- };
-
- enum {
- digit_buf_size = LDBL_MAX_10_EXP+200
- };
-
- // Fields defining digits to be converted, usually filled in by derived
- // class before calling one of the conversion methods below.
- int error; // Bit-field
- int radix; // 8, 10, or 16, 0=not yet determined
- bool is_signed; // Sign allowed in input
- bool negative; // Minus sign was present
- bool advanced; // Iterator was advanced
- bool reached_end; // Needed a char, but iterator matched end
- bool exp_negative; // Minus sign was present on exponent
- char *frac_beg; // Start of fractional part of digits
- char *exp_beg; // Start of exponent in digits
- char digits[digit_buf_size]; // Binary digits (i.e. 0,1,2 not '0','1','2')
-
- digit_reader_base (void): error(good), radix(0),
- is_signed(true), negative(false), advanced(false), reached_end(false)
- { }
-
- unsigned long to_ulong (char *end);
- unsigned long to_ulong_dec (char *beg,char *end);
-
- #ifdef _RWSTD_LONG_LONG
- unsigned _RWSTD_LONG_LONG to_ulong_ulong (char *end);
- #endif
-
- void *to_pointer (char *end);
- float to_float (char *end);
- double to_double (char *end);
- long double to_long_double (char *end);
-
- private:
- void to_floating_point (char *end, char *pattern, void *result);
- };
-
- // ------------------------------------------------------------
- // Implementation class template -- digit_reader_base_1<charT>.
- // ------------------------------------------------------------
-
- // Contains parts of digit_reader that depend on charT but not on the iterator
- // type.
-
- template <class charT>
- class _RWSTDExportTemplate digit_reader_base_1 :
- public digit_reader_base,
- public digit_handler_base_1<charT>
- {
- public:
- typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
-
- protected:
- // Constructor for general numerics, gets punct from loc's numpunct<charT>.
- digit_reader_base_1 (const locale &loc);
-
- // Constructor for use by derived money_reader, caller passes punct, which
- // in fact is loc's moneypunct<charT,true|false> facet.
- digit_reader_base_1 (const locale &loc, const punct_data<charT>&);
-
- // These could be moved to digit_handler_base_1<charT> ...
- charT get_decimal_point() const { return this->punct.dp_; }
- charT get_thousands_sep() const { return this->punct.ts_; }
- const char *get_grouping() const { return this->punct.gr_.c_str(); }
- };
-
- // -------------------------------------------------------------------
- // Implementation class template -- digit_reader<charT,InputIterator>.
- // -------------------------------------------------------------------
-
- // Facets that do numeric input use temporary objects of this class to
- // extract keywords and strings of digits and punctuation from the input
- // stream.
-
- template <class charT,class InputIterator>
- class _RWSTDExportTemplate digit_reader :
- public digit_reader_base_1<charT>
- {
- public:
- // Constructor for reading general numerics.
- digit_reader (InputIterator& in,InputIterator& end,
- ios_base &io);
-
- protected:
- // Constructor for use by derived money_reader class.
- digit_reader (InputIterator& in,InputIterator& end,
- ios_base &io,const punct_data<charT>&);
-
- public:
- char *get_int_digits (void); // Get integer-format digits
- char *get_float_digits (void); // Get floating point-format digits
- char *get_pointer_digits (void); // Get pointer-format digits
-
- // Parse keyword from input, return associated integer value
- int get_keyword (const keyword_map<charT>&);
-
- protected:
- ios_base &io;
- InputIterator& in;
- InputIterator& end;
-
- const char *grouping;
- charT thousands_sep;
- char groups[40];
- char *gpos;
-
- // Helper functions
- char *get_digit_string (char*); // Get a string of digits
- char *get_digit_groups (char*); // Get grouped digits (recursive)
-
- inline bool at_end (void) { // Test for end of stream, and
- if (in==end) { // remember whether end was
- this->reached_end=true; // ever reached
- return true;
- }
- return false;
- }
- };
-
-
- // ------------------------------------------
- // Implementation class -- digit_writer_base.
- // ------------------------------------------
-
- // Contains the parts of digit_writer that do not depend on the template
- // parameters.
-
- class _RWSTDExportTemplate digit_writer_base :
- public digit_map_base
- {
- public:
- digit_writer_base (ios_base &b);
-
- void get_pattern(char *p, char *prefix, bool is_signed);
- bool get_f_pattern(char *p, char prefix);
- void digitize (void*);
- void digitize (unsigned short);
- void digitize (short);
- void digitize (unsigned int);
- void digitize (int);
- void digitize (unsigned long);
- void digitize (long);
- #ifdef _RWSTD_LONG_LONG
- void digitize (unsigned _RWSTD_LONG_LONG);
- void digitize (_RWSTD_LONG_LONG);
- #endif
- void digitize (double);
- #ifndef _RWSTD_NO_LONG_DOUBLE
- void digitize (long double);
- #endif
-
- ios_base &io;
- ios_base::fmtflags flags;
- int precision,width,radix,iprecision,num_groups;
- enum { left, internal, right } adjust;
- bool fractional,separable;
- char *start,*end,*group;
- char buffer[400];
- char groups[150];
-
- int calc_groups (int,const char*);
- };
-
- // ------------------------------------------------------------
- // Implementation class template -- digit_writer_base_1<charT>.
- // ------------------------------------------------------------
-
- // Contains parts of digit_writer<charT,OutputWriter> that depend only on the
- // charT parameter.
-
- template <class charT>
- class _RWSTDExportTemplate digit_writer_base_1 :
- public digit_writer_base,
- public digit_handler_base_1<charT>
- {
- public:
- typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
-
- protected:
- // Constructor for general numerics, gets punct from loc's numpunct<charT>.
- digit_writer_base_1
- (ios_base&,const locale&);
-
- // Constructor for money, passed punct is really a moneypunct<charT,Intl>.
- digit_writer_base_1 (ios_base&,const punct_data<charT>&);
-
- // These could be moved to digit_handler_base_1<charT> ...
- charT get_decimal_point() const { return this->punct.dp_; }
- charT get_thousands_sep() const { return this->punct.ts_; }
- const char *get_grouping() const { return this->punct.gr_.c_str(); }
- };
-
- // --------------------------------------------------------------------
- // Implementation class template -- digit_writer<charT,OutputIterator>.
- // --------------------------------------------------------------------
-
- // Facets that do numeric output use temporary objects of this type (on the
- // stack) to convert numeric quantities into sequences of digits and other
- // punctuation.
-
- template <class charT,class OutputIterator>
- class _RWSTDExportTemplate digit_writer :
- public digit_writer_base_1<charT>
- {
- public:
- typedef basic_string<charT,char_traits<charT>,allocator<charT> > string_type;
-
- // Constructor for general numerics.
- digit_writer (OutputIterator&,ios_base&);
-
- protected:
- // Constructor for use by derived money_writer classes, passed punct_data is
- // really a moneypunct<charT,Intl> facet.
- digit_writer (OutputIterator&,ios_base&,
- const punct_data<charT>&);
-
- public:
- void put_digits (charT fill);
- void put_keyword (const string_type&,charT fill);
-
- protected:
- OutputIterator &out;
- };
-
-
- #ifndef _RWSTD_NO_NAMESPACE
- } // namespace __rwstd
- #endif
-
- #ifdef _RWSTD_COMPILE_INSTANTIATE
- #include <rw/numbrw.cc>
- #endif
-
- #pragma option pop
- #endif // __STD_RW_LOCNUMRW__
-